package cn.hp.controller;

import cn.hp.bean.Order;
import cn.hp.service.OrderService;
import cn.hp.utils.JsonData;
import cn.hp.utils.MapUtils;
import cn.hp.utils.RedisUtil;
import cn.hp.utils.SmsUtil;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/cphone")
public class CustomerPhoneController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private OrderService orderService;

    //判断是否登陆 如果登录提交表单 没有登录回到登录页面
    @RequestMapping("/isLogin")
    @ResponseBody
    public JsonData isLogin(HttpSession session){
        String phone = (String) session.getAttribute("phone");

        if (phone == null || "".equals(phone)){
            return JsonData.buildError("请登录...");
        } else {
            return JsonData.buildSuc("请稍后...");
        }
    }

    @RequestMapping("/sendSms")
    @ResponseBody
    public JsonData sendSms(String phone, HttpSession session){
        // 获取验证码
        String fourRandom = SmsUtil.getFourRandom();

        System.out.println(fourRandom);

        // 发送短信
        //String s = SmsUtil.sendSms(phone, fourRandom);
        //System.out.println(s);
        //if (s.contains("OK")){
            // 存储验证码
            // session.setAttribute("checkCode", fourRandom);
            // 如果验证码有效时间1分钟如何处理
            redisUtil.setEx(phone, fourRandom, 20, TimeUnit.SECONDS);
            return JsonData.buildSuc(fourRandom + "");
        //} else {
            //return JsonData.buildError("短信异常...");
        //}
    }
    @RequestMapping("/login")
    @ResponseBody
    public JsonData login(String phone, String code, HttpSession session){
        // 验证验证码是否为空
        if (StringUtils.isEmpty(code)){
            return JsonData.buildError("请输入验证码....");
        }
        // 获取验证码信息
        //String checkCode = (String) session.getAttribute("checkCode");
        // 从redis缓存中获取验证码信息
        String checkCode = (String) redisUtil.get(phone);
        if (!code.equals(checkCode)){
            return JsonData.buildError("验证码输入错误，请重新输入....");
        }

        session.setAttribute("phone", phone);
        return JsonData.buildSuc("登陆成功....");
    }

    @RequestMapping("/getMoney")
    @ResponseBody
    public String getMoney(String lng, String lat, String oldLng, String oldLat){
        int money = (int) MapUtils.algorithm(Double.valueOf(lng), Double.valueOf(lat), Double.valueOf(oldLng), Double.valueOf(oldLat)) / 100;
        return money + "";
    }

    // 添加订单
    @RequestMapping("/addOrder")
    @ResponseBody
    public JsonData addOrder(Order order, HttpSession session){
        String phone = (String) session.getAttribute("phone");
        order.setStatus("0");
        order.setPhone(phone);
        order.setCreatetime(new Date());
        // 保存数据
        JsonData jsonData = orderService.addOrder(order);
        return jsonData;
    }

    @RequestMapping("/selectListByPhone")
    @ResponseBody
    public JsonData selectListByPhone(HttpSession session){
        String phone = (String) session.getAttribute("phone");
        JsonData jsonData = orderService.selectListByPhone(phone);
        return jsonData;
    }
}
